home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_07 / 2n07067a < prev    next >
Text File  |  1991-06-01  |  3KB  |  120 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include "esp.h"
  5. #include "esp.fu"
  6.  
  7. #define OK     1
  8. #define FAIL   0
  9. #define bool   short
  10.  
  11. #define LINE_CONTROL_REG  3
  12.  
  13. #define EIGHT_BITS   0x03
  14. #define NO_PARITY    0x00
  15. #define ONE_STOP_BIT 0x04
  16. #define SDU_SIZE     8L       /* 8 data + 1 start + 1 stop */
  17.  
  18. main(argc,argv)
  19. int argc;
  20. char **argv;
  21. {
  22.    ESP_Handle esp;
  23.    printf("Setting up for Enhanced Mode\n");
  24.  
  25.    if (!setup_handle_params(&esp))
  26.       return FAIL;
  27.  
  28.    if (!ESP_SetupEnhancedMode(&esp))
  29.       return FAIL;
  30.    printf("Setup complete.\n");
  31.    return OK;
  32. }
  33.  
  34.  
  35. int setup_handle_params(esp_h)
  36. ESP_Handle *esp_h;
  37. {
  38.    short port;
  39.    short esp_no;
  40.  
  41.    printf("Use which ESP board (1-2): ");
  42.    scanf("%d", &esp_no);
  43.    printf("Use which ESP port  (1-2): ");
  44.    scanf("%d", &port);
  45.    if ((esp_no < 1) || (esp_no > 2) ||
  46.        (port   < 1) || (port   > 2)) {
  47.       printf("Invalid ESP board and/or port no.\n");
  48.       return FAIL;
  49.       }
  50.  
  51.    if (!ESP_InitHandle(esp_h, esp_no, port))
  52.       return FAIL;
  53.  }
  54.  
  55. /* Simplified Enhanced Mode operation:
  56.     * No DMA
  57.     * 8 bits, no parity, 1 stop bit
  58.     * user-defined baud rate
  59.     * interrupt on receive FIFO, transmit FIFO, or error
  60.  
  61.    After this function returns, the application is ready to install
  62.    the serial interrupt handler as usual.. */
  63.  
  64. int ESP_SetupEnhancedMode(esp_h, baud_rate)
  65. ESP_Handle *esp_h;
  66. long baud_rate;
  67. {
  68.    long recv_tmo;
  69.  
  70.    /* This is the only time you ever need to call this func */
  71.    ESP_WriteToUART(esp_h, LINE_CONTROL_REG,
  72.       EIGHT_BITS | NO_PARITY | ONE_STOP_BIT);
  73.  
  74.    /* Disable INTs until everything is ready to go */
  75.    ESP_SetEnhancedIntAndDMA(esp_h, 0);
  76.  
  77.    /* Ignore if you're not using DMA */
  78.    ESP_SetDMAtimeout(esp_h, 0);
  79.  
  80.    if (esp_h->port == 1)
  81.       ESP_SetServiceRequestMask(esp_h, ESP_SERV_PORT1_RX_FIFO |
  82.           ESP_SERV_PORT1_TX_FIFO | ESP_SERV_PORT1_ERROR);
  83.    else
  84.       ESP_SetServiceRequestMask(esp_h, ESP_SERV_PORT2_RX_FIFO |
  85.           ESP_SERV_PORT2_TX_FIFO | ESP_SERV_PORT2_ERROR);
  86.  
  87.    /* Interrupt on all possible error and status changes */
  88.    ESP_SetErrorStatusMask(esp_h, ESP_ERR_ALL_BYTE_2,
  89.       ESP_ERR_ALL_BYTE_2);
  90.  
  91.    /* Flow control OFF at 768 chars, back on at 512 chars */
  92.    ESP_SetRXFlowControlLevels(esp_h, 768, 512);
  93.  
  94.    /* Trigger receive interrupt at 256 chars,
  95.              transmit interrupt at 768 chars */
  96.    ESP_SetFIFOTriggerLevels(esp_h, 256, 768);
  97.  
  98.    /* Suggested timeout is 4 x character transmission speed,
  99.       but no less than 2 ms  */
  100.    recv_tmo = ((baud_rate * 4L) / SDU_SIZE) / 1000L;
  101.    recv_tmo = min(recv_tmo, 2L);
  102.    ESP_SetReceiveCharTimeout(esp_h, (short)recv_tmo);
  103.  
  104.    /* Maximum time we want to be flowed off, sugg. 60 ms */
  105.    ESP_SetFlowedOffTimeout(esp_h, 60);
  106.  
  107.    ESP_SetBaudRate(esp_h, baud_rate);
  108.  
  109.    ESP_SetMode(esp_h, ESP_MODE_ENHANCED);  /* No DMA */
  110.  
  111.    /* Enable INTs and keep same IRQ as used in Comp. Mode */
  112.    if (esp_h->port == 1)
  113.       ESP_SetEnhancedIntAndDMA(esp_h, ESP_SET_ENH_INTR_ON |
  114.          ESP_SET_ENH_IRQ4 | ESP_SET_DMA_OFF);
  115.    else
  116.       ESP_SetEnhancedIntAndDMA(esp_h, ESP_SET_ENH_INTR_ON |
  117.          ESP_SET_ENH_IRQ3 | ESP_SET_DMA_OFF);
  118.    return OK;
  119. }
  120.